package com.lenovo.manufacture;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SupplyCommodity extends AppCompatActivity {
    private Button btReturn;
    private ImageButton ibMoney;
    private ImageButton ibNumber;
    private ListView lv;
    MyAdapter myAdapter;
    int k=0;
    int j=0;
    private final List<SupplyData.DataBean> data = new ArrayList<>();
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/index/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_supply_commodity);
        initView();
        getSupply();
    }

    private void initView() {
        ibMoney =findViewById(R.id.ib_money_sort);
        ibNumber =findViewById(R.id.ib_number_sort);
        btReturn =findViewById(R.id.bt_return);
        lv =findViewById(R.id.ll_supply);
    }

    private void setEvent(){
        btReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ibMoney.setOnClickListener(v -> {
            if (k==0){
                ibMoney.setImageResource(R.mipmap.up);
                Collections.sort(data, (o1, o2) -> o1.getPrice()-o2.getPrice());
                k=1;
                myAdapter.notifyDataSetChanged();
            }else {
                    ibMoney.setImageResource(R.mipmap.down);
                    Collections.sort(data, (o1, o2) -> o2.getPrice()-o1.getPrice());
                    k=0;
                    myAdapter.notifyDataSetChanged();
            }
        });
        ibNumber.setOnClickListener(v -> {
            if (j==0){
                ibNumber.setImageResource(R.mipmap.down);
                Collections.sort(data, (o1, o2) -> o2.getNum()-o1.getNum());
                j=1;
                myAdapter.notifyDataSetChanged();
            }else {
                ibNumber.setImageResource(R.mipmap.up);
                Collections.sort(data, (o1, o2) -> o1.getNum()-o2.getNum());
                j=0;
                myAdapter.notifyDataSetChanged();
            }
        });


    }

    private void getSupply(){
        Service service=retrofit.create(Service.class);
        Call<SupplyData> issupply=service.isSupply();
        issupply.enqueue(new Callback<SupplyData>() {
            @Override
            public void onResponse(Call<SupplyData> call, Response<SupplyData> response) {
                data.addAll(response.body().getData());
                myAdapter=new MyAdapter();
                lv.setAdapter(myAdapter);
                setEvent();
            }

            @Override
            public void onFailure(Call<SupplyData> call, Throwable t) {

            }
        });

    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_two,parent,false);
            }
            TextView tvCommodity = convertView.findViewById(R.id.tv_commodity);
            TextView tvMoney = convertView.findViewById(R.id.tv_money);
            TextView tvNumber = convertView.findViewById(R.id.tv_number);
            TextView tvSupplier = convertView.findViewById(R.id.tv_supplier);
            tvCommodity.setText(data.get(position).getMaterialName());
            tvMoney.setText(String.valueOf(data.get(position).getPrice()));
            tvNumber.setText(String.valueOf(data.get(position).getNum()));
            tvSupplier.setText(data.get(position).getSupplyName());
            return convertView;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}